package com.jcl.recruit.api.resume.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jcl.core.common.page.PageResult;
import com.jcl.core.resp.ApiResponse;
import com.jcl.recruit.resume.service.ResumeBaseService;
import com.jcl.recruit.resume.service.ResumeService;
import com.jcl.recruit.resume.vo.ResumeResultEntity;
import com.jcl.recruit.resume.vo.ResumeVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.springframework.beans.support.PagedListHolder.DEFAULT_PAGE_SIZE;


@RestController
@RequestMapping("/api/resume")
public class ResumeController {

    private static Logger log = LoggerFactory.getLogger(ResumeController.class);

    @Autowired
    private ResumeService resumeService;

    @Autowired
    private ResumeBaseService resumeBaseService;

    @RequestMapping("/user/page/list")
    public ApiResponse findList(@RequestBody ResumeVo resumeVo) {

        PageResult result = resumeService.pageListByUserId(resumeVo);
        return ApiResponse.success(result);
    }

    /**
     * 获取单个简历
     *
     * @param id
     * @return
     */
    @RequestMapping("/get/{id}")
    public ApiResponse resumeDetail(@PathVariable("id") Integer id) {

        ResumeResultEntity entity = resumeService.getResumeInfoDetail(id);
        return ApiResponse.success(entity);
    }

    /**
     * 简历接收列表
     *
     * @param keyword
     * @param education
     * @param sex
     * @param workyear
     * @param user_id         对应操作人
     * @param city
     * @param apply_date_from
     * @param apply_date_to
     * @param source_sub
     * @param status
     * @param toPage
     * @return
     */
    @RequestMapping("/resume/find")
    public Map<String, Object> findResume(String keyword, String education, String sex, Integer workyear, Integer user_id,
                                          String city, String apply_date_from, String apply_date_to, String source_sub, Integer status, Integer ismanual,
                                          @RequestParam(value = "page", required = false, defaultValue = "1") String toPage) {
        Date beginDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (StringUtils.isNotBlank(apply_date_from)) {
            beginDate = sdf.parse(apply_date_from, new ParsePosition(0));
            System.out.println(beginDate + "--------------");
        }
        Date endDate = null;
        if (StringUtils.isNotBlank(apply_date_to)) {
            endDate = sdf.parse(apply_date_to, new ParsePosition(0));
        }
        if (StringUtils.isBlank(city)) {
            city = null;
        }
        PageHelper.startPage(Integer.valueOf(toPage), DEFAULT_PAGE_SIZE);
        List<Map<String, Object>> resumes = resumeBaseService.selectQueryResumes(keyword, education, sex, workyear, city, beginDate, endDate, source_sub, status, user_id, ismanual);
        PageInfo pageInfo = new PageInfo(resumes);
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> entity = resumeBaseService.selectCountResume(keyword, education, sex, workyear, city, beginDate, endDate, source_sub, status, user_id, ismanual);
        Map<String, Object> cntMap = new HashMap<>();
        for (Map<String, Object> vo : entity) {
            if (null != vo.get("status")) {
                cntMap.put(vo.get("status").toString(), vo.get("num"));
            }
        }
        /*Map<String,String> params = new HashMap<>();
        params.put("keywords",keyword);
        template.convertAndSend("msg",params);*/
        map.put("resumes", pageInfo.getList());
        map.put("total", pageInfo.getTotal());
        map.put("type", "success");
        map.put("message", "执行成功");
        map.put("cnt", cntMap);
        return map;
    }


}
